<?php

class MemberModel extends Model{
	
	//定义表名
	public $table = 'user';
	//验证规则
	public $validate = array(
		array('username','nonull','用户名不能为空',2,3),
		array('password','nonull','密码不能为空',2,3),	
	);
	
	//验证登录
	public function validate_login(){
		//执行自动验证,如果验证不通过则返回假
		if(!$this->create()) return false;
		$username = Q('post.username');
		$password = Q('post.password','','md5');
		
		//找到用户
		$data = $this->where(array('username'=>$username))->find();
		
//		p($data);die;
		//如果用户输入的名字或密码不对则返回错误
		if(!$data || $password != $data['password']){
			$this->error='用户名或者密码错误';
			return false;
		}
		return $data;
	}
		//注册
		public function validate_register(){
//			echo 1;die;
			$username = Q('post.username');
			$password = Q('post.password');
			$password1 = Q('post.cpassword');
//			p($password);p($password1);
			//验证两次密码是否一样
			if($password != $password1){
				$this->error = '两次输入的密码不一样';
			}
			//检测用户名是否存在
			$data = $this->where(array('username'=>$username))->find();
			if($data){
				$this->error= "用户名已经存在";
				return false;
			}
			$data = array(
				'username' => Q('post.username'),
				'password' => Q('post.password','','md5')
			);
//			$data =$this->all();
//			p($data);die;
			$this->add($data);
//			$this->success('注册');
			return true;
			
		
		
		
		}
	}
	
	
	
